package com.oj.ojcodesandbox;

import cn.hutool.core.io.resource.ResourceUtil;
import com.oj.ojcodesandbox.model.CodeSandboxCmd;
import com.oj.ojcodesandbox.model.ExecuteCodeRequest;
import com.oj.ojcodesandbox.model.ExecuteCodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;


/**
 * cpp本机代码沙箱
 *
 * @author 韩帅
 * @since 2023/08/21
 */
@Slf4j
@Component
public class CppNativeCodeSandbox extends CodeSandboxTemplate {
    private static final String PREFIX = File.separator + "cpp";

    private static final String GLOBAL_CODE_DIR_PATH = File.separator + "tmpCode";

    private static final String GLOBAL_CPP_NAME = File.separator + "main.cpp";


//    public static void main(String[] args) {
//        CppNativeCodeSandbox cppNativeCodeSandbox = new CppNativeCodeSandbox();
//        ExecuteCodeRequest executeCodeRequest = new ExecuteCodeRequest();
//        executeCodeRequest.setInputList(Arrays.asList("1 2", "1 3"));
//        String code = ResourceUtil.readStr("testCode/simpleComputeArgs/main.cpp", StandardCharsets.UTF_8);
//        executeCodeRequest.setCode(code);
//        executeCodeRequest.setLanguage("cpp");
//        ExecuteCodeResponse executeCodeResponse = cppNativeCodeSandbox.executeCode(executeCodeRequest);
//        System.out.println(executeCodeResponse);
//    }

    public CppNativeCodeSandbox() {
        super.prefix = PREFIX;
        super.globalCodeDirPath = GLOBAL_CODE_DIR_PATH;
        super.globalCodeFileName = GLOBAL_CPP_NAME;
    }

    @Override
    public CodeSandboxCmd getCmd(String userCodeParentPath, String userCodePath) {
        return CodeSandboxCmd
                .builder()
                .compileCmd(String.format("g++ -finput-charset=UTF-8 -fexec-charset=UTF-8 %s -o %s", userCodePath, userCodePath.substring(0, userCodePath.length() - 4)))
                .runCmd(userCodeParentPath + File.separator + "main")
                .build();
    }
}
